package com.example.web;


import com.example.fanqie.entity.Author;
import com.example.fanqie.entity.Chapter;
import com.example.fanqie.entity.Novel;
import com.example.fanqie.entity.User;
import com.example.fanqie.mapper.AuthorMapper;
import com.example.fanqie.mapper.ChapterMapper;
import com.example.fanqie.mapper.NovelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("author")
public class AuthorDao {

    @Resource
    private AuthorMapper authorMapper;


    @RequestMapping("selectAuthor")
    public Author selectAuthor(@RequestParam("authorId") Integer authorId) {
        Author author1 = authorMapper.selectauthorId(authorId);

        return  author1;

    }


    @RequestMapping("/login")
    public Integer login(@RequestParam("phone") String aPhone){
        if(aPhone == null || aPhone.isEmpty()){
            return  0;
        }
        Author login = authorMapper.login(aPhone);
        if(login != null){

            return login.getAuthorId();
        }


        Author author = new Author();
        author.setAPhone(aPhone);


        author.setAuthorName("作家"+aPhone);
        int user1 = authorMapper.insert(author);
        if(user1 == 1){
            Author login1 = authorMapper.login(aPhone);

            return login1.getAuthorId();
        }
        return 0;

    }


    @Autowired
    StringRedisTemplate redisTemplate;



    @RequestMapping("iflogin")
    public Object iflogin( HttpServletRequest request){
        if(request.getHeader("authorId") == null){
            return null;
        }
        return request.getHeader("authorId");

    }

    @RequestMapping("logout")
    public void logout(){
        redisTemplate.delete("authorjwt");

    }

    @RequestMapping("getAuthor")
    public Author getAuthor(@RequestParam("authorId")  Integer authorId){
        Author author = authorMapper.selectauthorId(authorId);

        return  author;
    }

    @Resource
    private NovelMapper novelMapper;

    @RequestMapping("selectNovel")
    public List<Novel> selectNovel(@RequestParam("authorId")  Integer authorId) {
        List<Novel> novels = novelMapper.selectNovel(authorId);
        return  novels;
    }

    @Resource
    private ChapterMapper chapterMapper;
    @RequestMapping("selectChapter")
    public Novel selectChapter(@RequestParam("novelId")  Integer novelId) {
        List<Chapter> selectchapterlist = chapterMapper.selectchapterlist(novelId);
        Novel novel =  new Novel();


        if(selectchapterlist != null && selectchapterlist.size() > 0){
            novel.setChapterNum(selectchapterlist.size());
            novel.setZuixin(selectchapterlist.get(novel.getChapterNum()-1).getChapterTitle());
            AtomicInteger x = new AtomicInteger(0);
            selectchapterlist.forEach(chapter -> {
                x.addAndGet(chapter.getCount());
            });
            novel.setWordCount(x.get());
            return  novel;
        }


        return  null;


    }





}
